package service;

import Dao.*;
import model.*;
import utils.*;
import service.GoodsService;

import java.sql.*;
import java.util.List;

public class OrderService {
    private OrderDao oDao = new OrderDao();
    public void addOrder(Order order) {
        Connection con = null;
        try {
            con = DataSourceUtils.getConnection();
            con.setAutoCommit(false);

            oDao.insertOrder(con, order);
            int id = oDao.getLastInsertId(con);
            order.setId(id);
            for(OrderItem item : order.getItemMap().values()) {
                oDao.insertOrderItem(con, item);
            }

            con.commit();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            if(con!=null)
                try {
                    con.rollback();
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
        }
    }
    public List<Order> selectAll(int userid){
        List<Order> list=null;
        GoodsService gService = new GoodsService();
        try {
            list = oDao.selectAll(userid);
            for(Order o :list) {
                List<OrderItem> l = oDao.selectAllItem(o.getId());
                Goods goods = gService.getGoodsById(o.getProduct_id());
                o.setGoods(goods);
                o.setItemList(l);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
    }
    public List<Order> GetAllOrder()
    {
        List<Order> list=null;
        try {
            list=oDao.GetAllOrder();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    public List<Order> selectAllBySellerId(int seller_id){
        List<Order> list=null;
        GoodsService gService = new GoodsService();
        try {
            list = oDao.selectAllBySellerId(seller_id);
            for(Order o :list) {
                List<OrderItem> l = oDao.selectAllItem(o.getId());
                Goods goods = gService.getGoodsById(o.getProduct_id());
                o.setGoods(goods);
                o.setItemList(l);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
    }
//    public Page getOrderPage(int status,int pageNumber) {
//        Page p = new Page();
//        p.setPageNumber(pageNumber);
//        int pageSize = 10;
//        int totalCount = 0;
//        try {
//            totalCount = oDao.getOrderCount(status);
//        } catch (SQLException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//        p.SetPageSizeAndTotalCount(pageSize, totalCount);
//        List list=null;
//        try {
//            list = oDao.selectOrderList(status, pageNumber, pageSize);
//            for(Order o :(List<Order>)list) {
//                List<OrderItem> l = oDao.selectAllItem(o.getId());
//                o.setItemList(l);
//            }
//        } catch (SQLException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//        p.setList(list);
//        return p;
//    }
    public void delete(int id) {
        Connection con = null;
        try {
            con = DataSourceUtils.getDataSource().getConnection();
            con.setAutoCommit(false);

            oDao.deleteOrderItem(con, id);
            oDao.deleteOrder(con, id);
            con.commit();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            if(con!=null)
                try {
                    con.rollback();
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
        }


    }
}
